package com.eorchis.module.user.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import com.eorchis.core.basedao.base.IBaseDao;
import com.eorchis.core.basedao.condition.BaseCondition;
import com.eorchis.module.department.domain.Department;
import com.eorchis.module.user.dao.IUserInfoDao;
import com.eorchis.module.user.dao.require.UserInfoRequire;
import com.eorchis.module.user.domain.User;
import com.eorchis.module.user.domain.UserInfoCondition;
import com.eorchis.module.user.domain.jsonbean.UserJsonBean;

/** @deprecated */
@Repository("com.eorchis.module.user.dao.impl.UserInfoDaoImpl")
public class UserInfoDaoImpl implements IUserInfoDao {

	@Autowired
	@Qualifier("com.eorchis.core.basedao.base.impl.BaseDao")
	private IBaseDao<User> baseDao;

	@Autowired
	@Qualifier("com.eorchis.module.user.dao.require.UserInfoRequire")
	private UserInfoRequire require;

	public List<User> getUserInfo(UserInfoCondition condition) throws UsernameNotFoundException {
		this.require.queryCheckUserLoginHQL(condition);

		condition.setStart(-1);
		condition.setRows(-1);
		return (List) this.baseDao.pagedQuery(condition);
	}

	public List<Department> getCurrentDepartment(UserInfoCondition condition) throws Exception {
		this.require.currentDepartmentHQL(condition);

		condition.setStart(-1);
		condition.setRows(-1);
		return (List) this.baseDao.pagedQuery(condition);
	}

	public List<Department> getCurrentManageScope(UserInfoCondition condition) throws Exception {
		this.require.currentManageScopeHQL(condition);

		condition.setStart(-1);
		condition.setRows(-1);
		return (List) this.baseDao.pagedQuery(condition);
	}

	public void discardOrReuseUser(UserInfoCondition condition) throws Exception {
		this.baseDao.executeUpdate(this.require.discardOrReuseUserHQL(condition));
	}

	public User getUser(UserInfoCondition condition) throws Exception {
		return (User) this.baseDao.findObject(User.class, condition.getSearchUserId());
	}

	public List<User> getUserList(UserInfoCondition condition) throws Exception {
		this.require.listUser(condition);

		return (List) this.baseDao.pagedQuery(condition);
	}

	public void updateUser(User baseUser) throws Exception {
		this.baseDao.updateEntityByPK(baseUser);
	}

	public void addUser(User baseUser) throws Exception {
		this.baseDao.addEntity(baseUser);
	}

	public User viewUser(UserInfoCondition condition) throws Exception {
		this.require.listUser(condition);
		List result = this.baseDao.find(condition.getSql(), condition.getParameterList().toArray());
		if ((result != null) && (result.size() > 0)) {
			return (User) result.get(0);
		}
		return null;
	}

	public List<UserJsonBean> listUser(UserInfoCondition condition) throws Exception {
		this.require.listUser(condition);

		condition.setBasebean(UserJsonBean.class);
		
		List list = (List) this.baseDao.pagedQueryJDBC(condition);
		return list;
	}

	public List<UserJsonBean> listRoleByUserId(String searchUserId) throws Exception {
		UserInfoCondition condition = this.require.listRoleByUserId(searchUserId);
		condition.setBasebean(UserJsonBean.class);
		condition.setStart(-1);
		condition.setRows(-1);
		List list = (List) this.baseDao.pagedQuery(condition);
		return list;
	}

	public Long listUserCount(UserInfoCondition condition) throws Exception {
		this.require.countUserHql(condition);
		return Long.valueOf(this.baseDao.countQuery(condition));
	}

	public User getUserByID(String id) throws Exception {
		User baseUser = (User) this.baseDao.findObject(User.class, id);
		this.baseDao.getHibernateSession().clear();
		return baseUser == null ? new User() : baseUser;
	}

	public List<User> getUserListByIDArray(String[] idArray) throws Exception {
		this.baseDao.getHibernateSession().clear();
		BaseCondition condition = this.require.getUserListByIDArrayHQL(idArray);
		return (List<User>) this.baseDao.find(condition.getSql(), condition.getParameterList().toArray());
	}
}